﻿(function () {
    var _inited = false;
    var _handler = "../Handlers/ModuleInfoShowHandler.ashx";
    var _dochandler = "../Handlers/DocumentInfoHandler.ashx";
    var _moduleSubID = null;
    var _menuname = null;
    var _tbhead = [];
    var _pagefirst = 0;
    var _pagerow = 20;
    var _pagejump = 1;
    var _recordnum = 0;
    var _pagenum = 0;
    var _querystr = '';
    var _editbtn = '<span class="spanEdit">修改</span>';
    var _deletebtn = '<span class="spanDelete">删除</span>';
    var _detailbtn = '<span class="spanDetail">详细</span>';

    function _init() {
        _moduleSubID = $.f_getQueryStringByName("ModuleSubID");
        _menuname = decodeURI($.f_getQueryStringByName("MenuName"));
        if (_moduleSubID == null) {
            alert('子模块不存在');
        }
        else {
            _initUI();
            _getData();
        }
    }

    function _initUI() {
        $('#btnInsertModuleInfo', 'table.tbhead').die().live('click', function () {
            var callback = function (params) {
                if (params && params != null) {
                    var cinfo = {};
                    cinfo.ModuleSubID = _moduleSubID;
                    cinfo.SqlString = params;
                    _InsertData(cinfo);
                }
            };
            top.IndexScript.ModuleInfoAddDialog.Open(null, callback);
        });
        $('.Wdate', 'table.tbhead').die().live('click', function () {
            WdatePicker({ isShowClear: false, readOnly: true, dateFmt: 'yyyy-MM-dd' });
        });
        $('.wdQuery', 'table.tbhead').die().live('click', function () {
            var info = {};
            var txtctr = $(this);
            info.FilePath = txtctr.val();
            info.FileData = txtctr.data('data');
            var callback = function (params) {
                if (params != null) {
                    txtctr.val(params.FilePath);
                    txtctr.data('data', params.FileData);
                }
            };
            top.IndexScript.DocumentInfoDialog.Open(info, callback);
        });
        $('.wdbtnQuery', 'table.tbhead').die().live('click', function () {
            var info = {};
            var txtctr = $(this).prev();
            info.FilePath = txtctr.val();
            info.FileData = txtctr.data('data');
            var callback = function (params) {
                if (params != null) {
                    txtctr.val(params.FilePath);
                    txtctr.data('data', params.FileData);
                }
            };
            top.IndexScript.DocumentInfoDialog.Open(info, callback);
        });
        $('.csQuery', 'table.tbhead').die().live('click', function () {
            var txtctr = $(this);
            var info = {};
            info.CompanyName = txtctr.val();
            var callback = function (params) {
                txtctr.data('data', params);
                if (params != null) {
                    txtctr.val(params.CompanyName);
                }
            };
            top.IndexScript.CompanyInfoDialog.Open(info, callback);
        });
        $('.csbtnQuery', 'table.tbhead').die().live('click', function () {
            var txtctr = $(this).prev();
            var info = {};
            info.CompanyName = txtctr.val();
            var callback = function (params) {
                txtctr.data('data', params);
                if (params != null) {
                    txtctr.val(params.CompanyName);
                }
            };
            top.IndexScript.CompanyInfoDialog.Open(info, callback);
        });
        $('#ulQueryModuleInfo').empty();
        $('#tbQueryModuleInfo thead').empty();
        $('#tbQueryModuleInfo tbody').empty();
        $('#btnQueryModuleInfo', 'table.tbhead').die().live('click', function () {
            _pagefirst = 0;
            _pagejump = 1;
            _querystr = _GetQueryString();
            _ExecuteQuery();
        });
        $('#btnFirst').die().live('click', function () {
            _pagefirst = 1;
            _pagejump = 1;
            _ExecuteQuery();
        });
        $('#btnPre').die().live('click', function () {
            _pagefirst = 1;
            if (_pagejump > 1) {
                _pagejump = _pagejump - 1;
            }
            _ExecuteQuery();
        });
        $('#btnNext').die().live('click', function () {
            _pagefirst = 1;
            if (_pagejump < _pagenum) {
                _pagejump = _pagejump + 1;
            }
            _ExecuteQuery();
        });
        $('#btnLast').die().live('click', function () {
            _pagefirst = 1;
            _pagejump = _pagenum;
            _ExecuteQuery();
        });
        $('#btnGo').die().live('click', function () {
            var gopage = $.trim($('#txtPageNum').val());
            if (gopage == '') {
                alert('请输入跳转页');
            }
            else {
                _pagefirst = 1;
                if (parseInt(gopage) < 1) {
                    _pagejump = 1;
                }
                else if (parseInt(gopage) > _pagenum) {
                    _pagejump = _pagenum;
                }
                else {
                    _pagejump = parseInt(gopage);
                }
                _ExecuteQuery();
            }
        });
        $('.spanEdit').die().live('click', function () {
            var trdata = $(this).closest('tr').data('data');
            if (trdata && trdata != null) {
                var params = {};
                params.ModuleSubID = _moduleSubID;
                params.AutoID = trdata.AutoID;
                $.ajax({
                    type: "post",
                    url: _handler,
                    data: "action=QueryModuleSubTableAutoID&Params=" + encodeURIComponent($.toJSON(params)),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                            function (data, textStatus) {
                                var callback = function (params) {
                                    if (params && params != null) {
                                        var cinfo = {};
                                        cinfo.ModuleSubID = _moduleSubID;
                                        cinfo.SqlString = params;
                                        cinfo.AutoID = trdata.AutoID;
                                        _SaveData(cinfo);
                                    }
                                };
                                top.IndexScript.ModuleInfoAddDialog.Open(data, callback, 'Edit');
                            },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
                    }
                });
            }

        });
        $('.spanDelete').die().live('click', function () {
            if (confirm('确定删除该数据吗？')) {
                var trdata = $(this).closest('tr').data('data');
                if (trdata && trdata != null) {
                    var params = {};
                    params.ModuleSubID = _moduleSubID;
                    params.AutoID = trdata.AutoID;
                    _DeleteData(params);
                }
            }
        });
        $('.spanDetail').die().live('click', function () {
            var trdata = $(this).closest('tr').data('data');
            if (trdata && trdata != null) {
                var params = {};
                params.ModuleSubID = _moduleSubID;
                params.AutoID = trdata.AutoID;
                $.ajax({
                    type: "post",
                    url: _handler,
                    data: "action=QueryModuleSubTableAutoID&Params=" + encodeURIComponent($.toJSON(params)),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        top.IndexScript.ModuleInfoAddDialog.Open(data, null, 'Select');
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
                    }
                });
            }
        });

        $('.spanDownload').die().live('click', function () {
            var filePath = $(this).html();
            window.open(_dochandler + "?action=DownloadFiles&filepath=" + encodeURIComponent(filePath));
        });
    }

    function _handlerMsg(msg) {
        switch (msg) {
            case 0:
                alert('操作成功');
                $('#btnQueryModuleInfo', 'table.tbhead').trigger('click');
                break;
            case 100:
                alert('模块还未初始化');
                break;
            default:
                alert('操作失败');
                break;
        }
    }

    function _getData() {
        $.ajax({
            type: "get",
            url: _handler + "?action=GetModuleSubFieldInfo&Params=" + encodeURIComponent(_moduleSubID) + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _bindData(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _bindData(data) {
        _bindQuery(data);
        _bindResult(data);
        top.IndexScript.ModuleInfoAddDialog.Init(data, _menuname);
        _ExecuteQuery();
    }

    function _bindQuery(data) {
        if (data) {
            for (var i = 0; i < data.length; i++) {
                var d = data[i];
                if (d.QueryFlag == 1 && d.DeleteFlag > 1) {
                    var li = $('<li>').html(d.FieldName).data('data', d);
                    if (d.SpecialFlag > 0) {
                        switch (d.SpecialFlag) {
                            case 1:
                                {
                                    //文档路径
                                    var str = '<input type="text" class="wdQuery valueQuery" readonly="readonly" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '" ';
                                    str += '/>';
                                    str += '<input type="button" class="wdbtnQuery" value="..." />';
                                    li.append(str);
                                }
                                break;
                            case 2:
                                {
                                    //厂商信息
                                    var str = '<input type="text" class="csQuery valueQuery" readonly="readonly" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '" ';
                                    str += '/>';
                                    str += '<input type="button" class="csbtnQuery" value="..." />';
                                    li.append(str);
                                }
                                break;
                            case 3:
                                {
                                    //所属专业
                                    var zyinfo = top.IndexScript.getSystemParameter('ProfessionalInfo');
                                    var str = '<select class="zyQuery valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '">';
                                    str += '<option></option>';
                                    if (zyinfo != null) {
                                        for (var n = 0; n < zyinfo.length; n++) {
                                            str += '<option>' + zyinfo[n].GeneralValue1 + '</option>';
                                        }
                                    }
                                    str += '</select>';
                                    li.append(str);
                                }
                                break;
                            case 4:
                                {
                                    //文件类型
                                    var wjinfo = top.IndexScript.getSystemParameter('FileType');
                                    var str = '<select class="wjQuery valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '">';
                                    str += '<option></option>';
                                    if (wjinfo != null) {
                                        for (var n = 0; n < wjinfo.length; n++) {
                                            str += '<option>' + wjinfo[n].GeneralValue1 + '</option>';
                                        }
                                    }
                                    str += '</select>';
                                    li.append(str);
                                }
                                break;
                            default:
                                {
                                    var str = '<input type="text" class="cQuery valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '" ';
                                    str += '/>';
                                    li.append(str);
                                }
                                break;
                        }
                    }
                    else {
                        switch (d.FieldType) {
                            case 'I':
                                {
                                    var str = '<input type="text" class="iQuery valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '" ';
                                    str += 'onkeyup="if(isNaN(value))execCommand(\'undo\')" ';
                                    str += 'onafterpaste="if(isNaN(value))execCommand(\'undo\')" ';
                                    str += '/>';
                                    li.append(str);
                                }
                                break;
                            case 'D':
                                {
                                    var str = '<input type="text" class="Wdate dQueryBegin valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += '/>';
                                    str += '～';
                                    str += '<input type="text" class="Wdate dQueryEnd valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += '/>';
                                    li.append(str);
                                }
                                break;
                            case 'B':
                                {
                                    var str = '<select class="bQuery valueQuery" FieldType="' + d.FieldType + '">';
                                    str += '<option value="1" selected="selected">是</option>';
                                    str += '<option value="2">否</option>';
                                    str += '</select>';
                                    li.append(str);
                                }
                                break;
                            default:
                                {
                                    var str = '<input type="text" class="cQuery valueQuery" ';
                                    str += 'FieldType="' + d.FieldType + '" ';
                                    str += 'maxlength="' + d.FieldLen + '" ';
                                    str += '/>';
                                    li.append(str);
                                }
                                break;
                        }
                    }

                    $('#ulQueryModuleInfo').append(li);
                }
            }
        }
    }

    function _bindResult(data) {
        _tbhead = [];
        if (data) {
            var tr = $('<tr>');
            tr.append('<td>序号</td>');
            for (var i = 0; i < data.length; i++) {
                var d = data[i];
                if (d.ResultFlag == 1 && d.DeleteFlag > 1) {
                    var td = $('<td>').html(d.FieldName).data('data', d);
                    tr.append(td);
                    _tbhead.push(d);
                }
            }
            tr.append('<td>操作</td>');
            $('#tbQueryModuleInfo thead').append(tr);
        }
    }

    function _GetQueryString() {
        var str = '';
        var lilist = $('#ulQueryModuleInfo > li');
        if (lilist && lilist != null) {
            $(lilist).each(function () {
                var linode = $(this);
                var lidata = linode.data('data');
                if (lidata && lidata != null) {
                    var txtvalue = linode.find(".valueQuery");
                    if (txtvalue && txtvalue != null) {
                        if (lidata.FieldType == 'D') {
                            var dbegin = linode.find('.dQueryBegin');
                            var dend = linode.find('.dQueryEnd');
                            var vbegin = $.trim($(dbegin).val());
                            var vend = $.trim($(dend).val());
                            if ((vbegin != null && vbegin != '') || (vend != null && vend != '')) {
                                str += lidata.FieldType;
                                str += lidata.AutoID;
                                str += ',';
                                str += (vbegin != null && vbegin != '') ? vbegin : '2000-01-01';
                                str += ',';
                                str += (vend != null && vend != '') ? vend : '2050-01-01';
                                str += ';';
                            }
                        }
                        else {
                            var v = $.trim($(txtvalue).val());
                            if (v != null && v != '') {
                                str += lidata.FieldType;
                                str += lidata.AutoID;
                                str += ',';
                                str += v;
                                str += ';';
                            }
                        }
                    }
                }
            });
        }
        return str;
    }

    function _ExecuteQuery() {
        var request = {};
        request.ModuleSubID = _moduleSubID;
        request.QueryString = _querystr;
        request.PageFirst = _pagefirst;
        request.PageRow = _pagerow;
        request.PageJump = _pagejump;
        _QueryData(request);
    }

    function _QueryData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=QueryModuleSubTable&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _bindQueryResult(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _bindQueryResult(data) {
        $('#tbQueryModuleInfo tbody').empty();
        if (data && data != null) {
            if (_pagefirst < 1) {
                _recordnum = data.RecordNum;
                _pagenum = data.PageNum;
            }
            if (data.ModuleSubTable && data.ModuleSubTable != null) {
                for (var i = 0; i < data.ModuleSubTable.length; i++) {
                    var mst = data.ModuleSubTable[i];
                    var trnode = $('<tr>').data('data', mst);
                    if (mst && mst != null) {
                        trnode.append($('<td>').html(mst.DisplayID));
                        if (mst.KeyValue && mst.KeyValue != null) {
                            for (var j = 0; j < _tbhead.length; j++) {
                                var keys = _tbhead[j];
                                var values = mst.KeyValue[keys.FieldName];
                                switch (keys.FieldType) {
                                    case 'D':
                                        {
                                            values = values.substr(0, 10);
                                        }
                                        break;
                                    case 'B':
                                        {
                                            values = ((values == 1) ? '是' : '否');
                                        }
                                        break;
                                    default:
                                        {
                                        }
                                        break;
                                }
                                if (keys.SpecialFlag == 1) {
                                    //文档提供下载
                                    values = "<span class='spanDownload'>" + values + "</span>";
                                }
                                var tdnode = $('<td>').html(values);
                                trnode.append(tdnode);
                            }
                        }

                        trnode.append($('<td>').append(_editbtn).append(_deletebtn).append(_detailbtn));
                    }
                    $('#tbQueryModuleInfo tbody').append(trnode);
                }
            }
        }
        else {
            if (_pagefirst < 1) {
                _recordnum = 0;
                _pagenum = 0;
            }
        }
        _bindQueryResultUI();
    }

    function _bindQueryResultUI() {
        $('#spanPageJump').html(_pagejump);
        $('#spanPageNum').html(_pagenum);
        $('#spanRecordNum').html(_recordnum);

        if (_pagenum <= 1) {
            $('#btnFirst').attr('disabled', 'disabled');
            $('#btnPre').attr('disabled', 'disabled');
            $('#btnNext').attr('disabled', 'disabled');
            $('#btnLast').attr('disabled', 'disabled');
        }
        else {
            if (_pagejump <= 1) {
                $('#btnFirst').attr('disabled', 'disabled');
                $('#btnPre').attr('disabled', 'disabled');
                $('#btnNext').removeAttr('disabled');
                $('#btnLast').removeAttr('disabled');
            }
            else if (_pagejump >= _pagenum) {
                $('#btnFirst').removeAttr('disabled');
                $('#btnPre').removeAttr('disabled');
                $('#btnNext').attr('disabled', 'disabled');
                $('#btnLast').attr('disabled', 'disabled');
            }
            else {
                $('#btnFirst').removeAttr('disabled');
                $('#btnPre').removeAttr('disabled');
                $('#btnNext').removeAttr('disabled');
                $('#btnLast').removeAttr('disabled');
            }
        }
        if (_recordnum > 0) {
            $('#btnGo').removeAttr('disabled');
        }
    }

    function _InsertData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=InsertModuleSubTable&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _DeleteData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=DelModuleSubTable&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _SaveData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SaveModuleSubTable&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    this.ModuleInfoShowMgr = {
        init:
        function () {
            if (!!_inited) return;
            _init();
            _inited = true;
        }
    }
})();
$(function () {
    ModuleInfoShowMgr.init();
});